/* OpenStereo Library - A library to support the development of real time stereoscopic applications.
 * Copyright (C) 2007  Pedro Leite.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <SL/sl.h>
#include "sl_global_variables.h"

SL_API void SL_API_ENTRY slCreateContext(void) {
	if (context) {
		error = SL_CONTEXT_ALREADY_CREATED;
		return;
	}
	context = (struct _SLContext*) malloc(
		sizeof(struct _SLContext));
	if (context) {
		context->plugins = g_hash_table_new(0, 0);
		context->viewports = g_hash_table_new(0, 0);
		context->initialized = SL_FALSE;
	} else {
		error = SL_CANNOT_CREATE_CONTEXT;
	}
}

SL_API void SL_API_ENTRY slDestroyPlugin(SLPlugin plugin) {
	if (plugin) {
		if (plugin->module) {
			if (plugin->unloadPlugin)
				plugin->unloadPlugin();
			if (g_module_close(plugin->module) == FALSE) {
				// TODO fazer algo;
			}
			plugin->loadPlugin = 0;
			plugin->getPluginName = 0;
			plugin->getPluginDescription = 0;
			plugin->allocateStereoData = 0;
			plugin->destroyStereoData = 0;
			plugin->renderStereo = 0;
			plugin->unloadPlugin = 0;
			plugin->name = 0;
			plugin->description = 0;
		}
		free(plugin);
	}
}

static gboolean removeAllPlugins(gpointer key, gpointer value, gpointer user_data) {
	slDestroyPlugin((SLPlugin) value);
	return TRUE;
}

static gboolean removeAllViewports(gpointer key, gpointer value, gpointer user_data) {
	slDestroyViewport((SLViewport) value);
	return TRUE;
}

SL_API void SL_API_ENTRY slDestroyContext(void) {
	if (context) {
		g_hash_table_foreach_remove(context->viewports, removeAllViewports, 0);
		g_hash_table_foreach_remove(context->plugins, removeAllPlugins, 0);
		g_hash_table_destroy(context->viewports);
		g_hash_table_destroy(context->plugins);
		context->initialized = SL_FALSE;
		context->viewports = 0;
		context->plugins = 0;
		free(context);
		context = 0;
	}
}

SL_API void SL_API_ENTRY slLoadPlugin(const char* filePath, SLuint* pluginID) {
	GModule* pluginModule = 0;
	SLPlugin plugin = 0;
	fprintf(stderr, "Plugin to be loaded: %s\n", filePath);

	pluginModule = g_module_open((const gchar*) filePath, (GModuleFlags) 0);

	if (pluginModule) {
		plugin = (SLPlugin) g_hash_table_lookup(context->plugins, pluginModule);

		if (!plugin) {
			plugin = (SLPlugin) calloc(1, sizeof(struct _SLPlugin));
			plugin->module = pluginModule;
			#define LOAD_FUNCTION(function) \
				if (g_module_symbol(pluginModule, #function, (gpointer*) &(plugin->function)) == FALSE) {\
					error = SL_INVALID_PLUGIN;\
					slDestroyPlugin(plugin);\
					return;\
				}
				LOAD_FUNCTION(loadPlugin)
				LOAD_FUNCTION(getPluginDescription)
				LOAD_FUNCTION(getPluginName)
				LOAD_FUNCTION(allocateStereoData)
				LOAD_FUNCTION(destroyStereoData)
				LOAD_FUNCTION(renderStereo)
				LOAD_FUNCTION(unloadPlugin)
				plugin->name = plugin->getPluginName();
				plugin->description = plugin->getPluginDescription();
			#undef LOAD_FUNCTION

			if (!plugin->loadPlugin()) {
				error = SL_CANNOT_OPEN_PLUGIN;
				return;
			}

			g_hash_table_insert(context->plugins, pluginModule, plugin);
			context->initialized = SL_TRUE;
		}
		*pluginID = (SLuint) ((long int) pluginModule);
	} else {
		error = SL_CANNOT_OPEN_PLUGIN;
	}
}

SL_API void SL_API_ENTRY slBindPlugin(SLuint pluginID) {
	SLPlugin plugin = 0;

	if (!context) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}

	plugin = (SLPlugin) g_hash_table_lookup(context->plugins,
	 	(gpointer) ((long int) pluginID));

	if (!plugin) {
		error = SL_INVALID_PLUGIN;
		return;
	}

	currentPlugin = plugin;
}

SL_API void SL_API_ENTRY slUnloadPlugin(SLuint pluginID) {
	SLPlugin plugin = 0;

	if (!context) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}

	plugin = (SLPlugin) g_hash_table_lookup(context->plugins,
	 	(gpointer) ((long int) pluginID));

	if (!plugin) {
		error = SL_INVALID_PLUGIN;
		return;
	}

	g_hash_table_remove(context->plugins, (gpointer) ((long int) pluginID));
	slDestroyPlugin(plugin);
}
